package com.example.gym_manage_system.view.backstage.loginView;

import com.example.gym_manage_system.entity.User;
import com.example.gym_manage_system.service.impl.UserServiceImpl;
import com.example.gym_manage_system.tools.GetProps;
import com.example.gym_manage_system.view.backstage.mainView.MainView;
import com.example.gym_manage_system.view.components.button.MyButton;
import com.example.gym_manage_system.view.components.panel.BackgroundImagePanel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.Properties;

/**
 * @author qzy
 * @description 描述
 * @create 2023-12-13 14:16
 */

@Component
public class LoginView extends JFrame {

    @Autowired
    private UserServiceImpl userService;

    private static final Properties props = GetProps.init();
    private static final String systemName = props.getProperty("system_name");
    private static final Integer loginWidth = Integer.parseInt(props.getProperty("login_width"));
    private static final Integer loginHeight = Integer.parseInt(props.getProperty("login_height"));
    private static final String primaryColor = props.getProperty("primary_color");

    private final SpringLayout springLayout = new SpringLayout();
    private final JLabel titleLabel = new JLabel(systemName);
    private final JLabel usernameLabel = new JLabel("用户名：");
    private final JTextField usernameField = new JTextField();
    private final JLabel passwordLabel = new JLabel("密   码：");
    private final JPasswordField passwordField = new JPasswordField();
    private final JPanel btnPanel = new JPanel();
    private final JButton loginBtn = MyButton.themeBtn("登录");

    public LoginView() {
        super(systemName);
        JPanel loginPanel = LoginPanel();
        this.add(loginPanel);

        // 登录按钮点击事件
        loginBtn.addActionListener(e ->  extracted());
//
        // 键盘回车登录
        loginPanel.getInputMap(JPanel.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false), "enter");
        loginPanel.getActionMap().put("enter", new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                extracted();
            }
        });

        // 窗口参数设置
        this.setSize(loginWidth, loginHeight);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
        getRootPane().setDefaultButton(loginBtn);
    }

    // 事件处理
    private void extracted() {
        String usernameInput = usernameField.getText();
        String passwordInput = new String(passwordField.getPassword());
        User user = userService.login(usernameInput);
        if (user.getPassword().equals(passwordInput)) {
            this.dispose();
            MainView.createMainView();
        }
    }

    public JPanel LoginPanel() {
        
        BackgroundImagePanel loginPanel = new BackgroundImagePanel("bg\\home1.jpg");
        loginPanel.setLayout(springLayout);

        titleLabel.setFont(new Font("楷体", Font.BOLD, 35));
        titleLabel.setForeground(Color.decode(primaryColor));

        Font labelFont = new Font("微软雅黑", Font.PLAIN, 20);
        Font textFont = new Font("微软雅黑", Font.PLAIN, 15);

        usernameLabel.setFont(labelFont);
        usernameField.setFont(textFont);
        usernameField.setOpaque(false);
        usernameField.setBackground(new Color(0, 0, 0, 0));
        usernameField.setPreferredSize(new Dimension(200, 30));

        passwordLabel.setFont(labelFont);
        passwordField.setFont(textFont);
        passwordField.setOpaque(false);
        passwordField.setBackground(new Color(0, 0, 0, 0));
        passwordField.setPreferredSize(new Dimension(200, 30));


        btnPanel.setOpaque(false);
        loginBtn.setPreferredSize(new Dimension(100, 30));
        btnPanel.add(loginBtn);

        loginPanel.add(btnPanel);
        loginPanel.add(titleLabel);
        loginPanel.add(usernameLabel);
        loginPanel.add(usernameField);
        loginPanel.add(passwordLabel);
        loginPanel.add(passwordField);

        setAttribute();
        return loginPanel;
    }

    public void setAttribute() {

        // 设置标题
        SpringLayout.Constraints titleLabelC = springLayout.getConstraints(titleLabel);
        titleLabelC.setX(Spring.constant((loginHeight / 2) - 80));
        titleLabelC.setY(Spring.constant(150));

        // 用户名
        springLayout.putConstraint(SpringLayout.NORTH, usernameLabel, 50, SpringLayout.SOUTH, titleLabel);
        springLayout.putConstraint(SpringLayout.WEST, usernameLabel, 20, SpringLayout.WEST, titleLabel);
        // 用户名输入框
        springLayout.putConstraint(SpringLayout.NORTH, usernameField, 0, SpringLayout.NORTH, usernameLabel);
        springLayout.putConstraint(SpringLayout.WEST, usernameField, 20, SpringLayout.EAST, usernameLabel);

        // 密码
        springLayout.putConstraint(SpringLayout.NORTH, passwordLabel, 20, SpringLayout.SOUTH, usernameLabel);
        springLayout.putConstraint(SpringLayout.EAST, passwordLabel, 0, SpringLayout.EAST, usernameLabel);
        // 密码输入框
        springLayout.putConstraint(SpringLayout.NORTH, passwordField, 0, SpringLayout.NORTH, passwordLabel);
        springLayout.putConstraint(SpringLayout.WEST, passwordField, 20, SpringLayout.EAST, passwordLabel);

        // 按钮
        springLayout.putConstraint(SpringLayout.NORTH, btnPanel, 30, SpringLayout.SOUTH, passwordLabel);
        springLayout.putConstraint(SpringLayout.WEST, btnPanel, 100, SpringLayout.WEST, passwordLabel);
    }
}
